<?php
/**
 *考勤系统常用计算类
 *年假等
 * */
class Tools_Tools {

    private static $instance;

    /**
     * 获取公司的年份信息<公司从2007年到现在>
     */
    public static function company_year($start_year = 2007) {
        $result = array();
        $now_year = date('Y');
        for($i = $start_year;$i<=$now_year;$i++) {
            $result[] = $i;
        }
        rsort($result);
        return $result;
    }

    /**
     * 创建月份
     */
    public static function company_month($year = 2007) {
        $result = array();
        $end_month = 12;
        $now_year = date('Y');
        if($year==$now_year) {
            $now_month = date('m');
            $now_month = substr($now_month,0,1)=='0' ? substr($now_month,1,1):$now_month;
            $end_month = $now_month;
        }
        for($i = 1;$i<=$end_month;$i++) {
            $result[] = $i;
        }
        return $result;
    }

    /**
     * 年假计算公式
     *
     * @param mixed $enterdate
     * @access public
     * @return int
     */
    public function annual($enterdate){
        $result = '';
        if(empty($enterdate)) {
            return $result;
        }
        $enter = strtotime($enterdate);
        if($enter == 0) {
            return $result;
        }
        $result = $this->annual_format($enter);
        return $result;
    }

    /**
     * 计算年假
     *
     * @param mixed $enterdate
     * @static
     * @access public
     * @return void
     */
    private function annual_format($enterdate) {
        $result = '0';
        if(empty($enterdate)) {
            return $result;
        }
        $now = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $totalday = ceil(($now - $enterdate)/86400);
        $year = intval($totalday/365);
        $extday = $totalday % 365;
        //年假加上一年的假期，从入职到现在所有的年假
        switch($year) {
            case '0':
                break;
            case '1':
                $result = 7 + $this->caculate_basic(9,$extday);
                break;
            case '2':
                $result = 16 + $this->caculate_basic(11,$extday);
                break;
            case '3':
                $result = 27 + $this->caculate_basic(13,$extday);
                break;
            case '4':
                $result = 40 + $this->caculate_basic(15,$extday);
                break;
            case '5':
                $result = 55 + $this->caculate_basic(15,$extday);
                break;
            default:
                $result = 55 + 15*($year-5) + $this->caculate_basic(15,$extday);
                break;
        }
        return $result;
    }

    /**
     * 根据工作年数计算所有的年假
     *
     * @param mixed $year
     * @static
     * @access public
     * @return void
     */
    public function annual_work_year($year) {
        $result = '0';
        if(empty($year)) {
            return $result;
        }
        //0表示工作第一年
        switch($year) {
            case '0':
                break;
            case '1':
                $result = 7;
                break;
            case '2':
                $result = 16;
                break;
            case '3':
                $result = 27;
                break;
            case '4':
                $result = 40;
                break;
            case '5':
                $result = 55;
                break;
            default:
                $result = 55 + 15*($year-5);
                break;
        }
        return $result;
    }

    /**
     * 清算年假
     *
     * @param mixed $enterdate
     * @param string $stop
     * @static
     * @access public
     * @return void
     */
    public function clear_annual_to_zero($enterdate,$stop) {
        $result = array();
        if(empty($enterdate) || empty($stop)) {
            return $result;
        }

        $enter = strtotime($enterdate);
        $stop = strtotime($stop);

        $now = time();
        //如果清算时间点与当前时间不够一年，不允许清算
        if (ceil((time() - $enter)/86400) <= 365) {
            return $result;
        }
        //如果入职在清零之后，不清算
        if ($enter > $stop) {
            return $result;
        }
        $now = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $totalday = ceil(($now - $enterdate)/86400);
        $year = intval($totalday/365);
        $annual = 0;
        $base = 7;
        $factor = 2;
        $pre_curr_annual = 0;
        $stop_leave_day = 0;
        $next_annual = 0;
        for ($i = 0;$i <= $year;$i++) {
            $next = mktime(0,0,0,date('m',$enter),date('d',$enter),date('Y',$enter) + $i);
            $tail = mktime(0,0,0,date('m',$enter),date('d',$enter),date('Y',$enter) + $i + 1);
            $curr_annual = $base + $factor * $i;
            //记录入职年度上一年的年假天数
            if($i >= 1) {
                $pre_curr_annual = $base + $factor * ($i - 1);
            }
            //如果当年年假大于15天，取15天
            if ($curr_annual > 15) {
                $curr_annual = 15;
            }
            //计算$stop年之的入职月-日之前的所有年假总数
            if ($next < $stop && $tail < $stop) {
                $annual += $curr_annual;
            } elseif($next < $stop && $tail > $stop) {
                //计算入职月份到清算年中止点余下的年假天数
                $stop_leave_day = intval(((($stop - $next)/86400)/365)*$curr_annual);//echo ($stop - $next)/86400/365,"+",date('Y-m-d',$stop),"+";
                //清算全年假 1月1日到入职月份，入职月份到年年底,必须入职超过2年
                if(ceil((time() - $enter)/86400) >= 365) {
                    $curr_first_day = mktime(0,0,0,1,1,date('Y',$enter) + $i);
                    $stop_one_year_leave_day = $pre_curr_annual*((($next - $curr_first_day)/86400 + 1)/365) + ((($stop - $next)/86400)/365)*$curr_annual;

                    if($pre_curr_annual - 2 >= 7 && date('Y') == 2013) {
                        $cut_2011_pre = ($pre_curr_annual - 2)*((($next - $curr_first_day)/86400 + 1)/365);
                        $cut_2011_pre_po = substr($cut_2011_pre, strpos($cut_2011_pre, '.')+1,2);
                        $cut_2011_end =  ((($stop - $next)/86400)/365)*($curr_annual-2);
                        $cut_2011_end_po = substr($cut_2011_end, strpos($cut_2011_end, '.')+1,2);
                        if($cut_2011_end_po+$cut_2011_pre_po >=100){
                            $stop_one_year_leave_day +=1;
                        }
                    }
                    $stop_one_year_leave_day = intval($stop_one_year_leave_day);
                } else {
                    $stop_one_year_leave_day = intval(((($stop - $next)/86400)/365)*$curr_annual);
                }
                $annual +=$stop_leave_day;
                //计算清算当年入职日到清算日时的年假天数
                $next_annual = $curr_annual;
                break;
            }
        }
        $result['annual'] = $annual;
        $result['left_annual'] = $stop_leave_day;
        $result['oneyear_leave_annual'] = $stop_one_year_leave_day;
        $result['next_annual'] = $next_annual;
        $result['last_day'] = ($stop - $next)/86400;
        $result['last_base'] = $curr_annual;
        $result['stop'] = date('Y-m-d H:i:s',$stop);
        return $result;
    }

    /**
     * 计算今年的年假天数，
     * 根据当前时间点计算
     * @param int $basic
     * @param int $expand
     * @access private
     * @return float
     */
    private function caculate_basic($basic,$expand) {
        $result = 0;
        if(empty($basic)) {
            return $result;
        }
        $expand = $basic*$expand/365;
        if($expand - intval($expand) >= 0.5) {
            $expand = intval($expand)+0.5;
        } else {
            $expand = intval($expand);
        }
        $result = intval($expand);
        return $result;
    }

    /**
     * 当月应工作天数
     *
     * @access public
     * @return void
     */
public function current_month_work_day($y = 0,$m = 0,$enter_date = false,$dimission_date = false) {
        if(empty($m)) {
            $m = date('m');
        }
        if(empty($y)) {
            $y = date('Y');
        }

        $start = mktime(0,0,0,$m,1,$y);
        $end = mktime(0,0,0,$m+1,1,$y);

        $days = ceil(($end - $start)/86400);

        $workday = 0;
        $holiday = $this->holiday_country();
        $each_year_holiday = $this->each_year_holiday_country();
        $special_workday = $this->special_work_day();
        for ($i = 1; $i<=$days; $i++) {
            $cacluate = mktime(0,0,0,$m,$i,$y);
            $date = date('Y-m-d',$cacluate);
            $weekday = date('w',$cacluate);

            if(in_array($date,$holiday)) {
                continue;
            }
            //加入条件判断，如果即是法定节假日，又是周末的统计
            if(in_array($date,$each_year_holiday) && ($weekday == 0 || $weekday == 6)) {
                $workday++;
                continue;
            }
            if(in_array($date,$special_workday)) {
                $workday++;
                continue;
            }
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $workday++;
        }
        return $workday;
    }

    /**
     * 获取当月未全勤天数
     *
     * @access public
     * @return void
     */
    public function current_month_not_work_day($y = 0,$m = 0,$enter_date = false,$dimission_date = false) {
        if(empty($m)) {
            $m = date('m');
        }
        if(empty($y)) {
            $y = date('Y');
        }
        /*$start = mktime(0,0,0,$m,1,$y);
        $end = mktime(0,0,0,$m+1,1,$y);

        $days = ceil(($end - $start)/86400);
        if (($y.$m == substr($enter_date, 0,6)) && ($y.$m == substr($dimission_date, 0,6))) {
            $days = intval(substr($enter_date, 6)) -1 + $days-intval(substr($dimission_date, 6));
        }elseif($y.$m == substr($enter_date, 0,6)){
            $days = intval(substr($enter_date, 6)) -1;
        }elseif ($y.$m == substr($dimission_date, 0,6)){
            $days = $days-intval(substr($dimission_date, 6));
        }else{
            return 0;
        }*/
        $start = $y.$m.'01';
        $end = $y.$m.date('t',mktime(0,0,0,$m,1,$y));
        //当月离职数据处理,离职日期到当月最后天数计算
        $curr_not_work = 0;
        if ($dimission_date > 0 && $y.$m == date('Ym',strtotime($dimission_date))) {
            $curr_not_work += Tools_Tools::get_instance()->get_many_current_action_day($dimission_date, date('Y-m-d',strtotime($end)),1);
        }
        //当月入职
        if ($enter_date > 0 && $y.$m == date('Ym',strtotime($enter_date))) {
            //退一天，原因:未包含第一天
            $curr_not_work += Tools_Tools::get_instance()->get_many_current_action_day(date('Y-m-d',strtotime($start)),$enter_date,0);
        }
        return $curr_not_work;
        $workday = 0;
        $holiday = $this->holiday_country();
        $special_workday = $this->special_work_day();
        for ($i = 1; $i<=$days; $i++) {
            $cacluate = mktime(0,0,0,$m,$i,$y);
            $date = date('Y-m-d',$cacluate);
            if(in_array($date,$holiday)) {
                continue;
            }
            if(in_array($date,$special_workday)) {
                $workday++;
                continue;
            }
            $weekday = date('w',$cacluate);
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $workday++;
        }
        return $workday;
    }


    /**
     * 当月实时工作天数，根据今天时间推算
     * @access public
     * @return void
     */
    public function actual_work_day_by_month($time = null) {
        $current = true;
        if(empty($time)) {
            $time = time();
            $current = false;
        }
        $month = date('m',$time);
        $start = mktime(0,0,0,$month,1,date('Y',$time));
        $end = mktime(0,0,0,$month+1,1,date('Y',$time));
        $days = ceil(($end - $start)/86400);
        $workday = 0;
        $holiday = $this->holiday_country();
        $special_workday = $this->special_work_day();
        for ($i = 1; $i<=$days; $i++) {
            $cacluate = mktime(0,0,0,$month,$i,date('Y',$time));
            if($cacluate <= mktime(0,0,0,date('m',$time),date('d',$time),date('Y',$time)) && $current) {
                continue;
            }
            if($cacluate >= mktime(0,0,0,date('m'),date('d'),date('Y'))) {
                break;
            }
            $date = date('Y-m-d',$cacluate);
            if(in_array($date,$holiday)) {
                continue;
            }
            if(in_array($date,$special_workday)) {
                $workday++;
                continue;
            }
            $weekday = date('w',$cacluate);
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $workday++;
        }
        return $workday;
    }

    /**
     * 当月离职人员实时工作天数，根据今天时间推算
     *
     * @access public
     * @return void
     */
    public function dissmion_work_day_by_month($time = null) {
        if(empty($time)) {
            $time = time();
        }
        $month = date('m',$time);
        $start = mktime(0,0,0,$month,1,date('Y',$time));
        $end = mktime(0,0,0,$month+1,1,date('Y',$time));
        $days = ceil(($end - $start)/86400);
        $workday = 0;
        $holiday = $this->holiday_country();
        $special_workday = $this->special_work_day();
        for ($i = 1; $i<=$days; $i++) {
            $cacluate = mktime(0,0,0,$month,$i,date('Y',$time));
            if($cacluate >= mktime(0,0,0,date('m',$time),date('d',$time),date('Y',$time))) {
                continue;
            }
            $date = date('Y-m-d',$cacluate);
            if(in_array($date,$holiday)) {
                continue;
            }
            if(in_array($date,$special_workday)) {
                $workday++;
                continue;
            }
            $weekday = date('w',$cacluate);
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $workday++;
        }
        return $workday;
    }

    /**
     * 计算实际的请假的天数,只在job/importHistoryLeaveData使用过一次
     * @param string $start
     * @param string $end
     * @return int
     * */
    public function how_many_day($start,$end) {
        $result = '';
        $nstart = $this->format_time($start);
        $nend = $this->format_time($end);
        $justday = ($nend - $nstart)/86400;
        //当天的假，不做处理
        if($justday == 0) {
            return $result;
        }
        //排除周末及特殊节假日
        $day = 0;
        $month = date('m',$nstart);
        $year = date('Y',$nstart);
        for ($i = 0; $i < $justday; $i++) {
            $cacluate = mktime(0,0,0,$month,$i+date('d',$nstart),$year);

            $date = date('Y-m-d',$cacluate);
            if(in_array($date,$this->holiday_country())) {
                continue;
            }
            if(in_array($date,$this->each_year_holiday_country())) {
                continue;
            }
            if(in_array($date,$this->special_work_day())) {
                $day++;
                continue;
            }
            $weekday = date('w',$cacluate);
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $day++;
        }
        $starthalf = substr($start,11,6);
        $endhalf = substr($end,11,6);
        //上下午相同时，加0.5天假
        if($starthalf == $endhalf) {
            $day +=0.5;
        } elseif($starthalf == '上午' && $endhalf == '下午') {
            //跨天的上下午加1
            $day +=1;
        } elseif($starthalf == '9点' && $endhalf == '18点') {
            $day +=1;
        }
        return $day;
    }
    /**
     * 新计算实际的请假的天数,现时间点改为9,14,18三个时间点
     * @param string $start
     * @param string $end
     * @return int
     * */
    public function get_many_day($start,$end) {
        $result = '';
        $nstart = $this->format_time($start);
        $nend = $this->format_time($end);
        $start_last = substr($start,11,5);
        $end_last = substr($end,11,5);
        $justday = ($nend - $nstart)/86400;
        //排除周末及特殊节假日
        $day = 0;
        $month = date('m',$nstart);
        $year = date('Y',$nstart);

        for ($i = 0; $i < $justday; $i++) {
            $cacluate = mktime(0,0,0,$month,$i+date('d',$nstart),$year);
            $date = date('Y-m-d',$cacluate);
            if(in_array($date,$this->each_year_holiday_country())) {
                continue;
            }
            if(in_array($date,$this->holiday_country())) {
                continue;
            }
            if(in_array($date,$this->special_work_day())) {
                $day++;
                continue;
            }
            $weekday = date('w',$cacluate);
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $day++;
        }
        $last_date = date('Y-m-d',$nend);//TODO Nc选假，最后一天选节假日
        $last_week = date('w',$nend);
        $last_day_ck = 1;
        if(!in_array($last_date,$this->special_work_day())){//最后一天yao
            if(in_array($last_date,$this->each_year_holiday_country()) || in_array($last_date,$this->holiday_country()) || ($last_week == 0 || $last_week == 6)){
                $last_day_ck = 0;
            }
        }
        if($start_last == '09:00' && $end_last == '18:00' && $last_day_ck) {
            $day += 1;
        } elseif($start_last == '09:00' && $end_last == '14:00') {
            $day += 0.5;
        } elseif($start_last == '14:00' && $end_last == '18:00') {
            $day += 0.5;
        } elseif($start_last == '14:00' && $end_last == '9:00') {
            $day += 0.5;
        }elseif($start_last == '18:00' && $end_last == '18:00') {
            $day += 1;
        } else {
            //不做处理
        }

        return $day;
    }

    /**
     * 获取当月离职，入职人员之间的未工作天数
     * @param string $start
     * @param string $end
     * @param string $type 0入职 1离职
     * @return int
     * */
    public function get_many_current_action_day($start,$end,$type=0) {
        //$start='2012-10-01';
        //$end='2012-10-09';
        $result = '';
        $nstart = $this->format_time($start);
        $nend = $this->format_time($end);
        $start_last = substr($start,11,5);
        $end_last = substr($end,11,5);
        $justday = ($nend - $nstart)/86400;

        //排除周末及特殊节假日
        $day = 0;
        $month = date('m',$nstart);
        $year = date('Y',$nstart);
        $holiday = $this->each_year_holiday_country();
        $take_holiday=$this->holiday_country();//特殊放假的日子
        for($cacluate=$nstart;$cacluate<$nend;$cacluate+=86400){
            $date = date('Y-m-d',$cacluate);
            $weekday = date('w',$cacluate);
            if(in_array($date,$holiday) && ($weekday == 0 || $weekday == 6)) {
                $day++;
                continue;
            }
            if(in_array($date,$take_holiday)) {
                //$day++; add 2012 10 25
                continue;
            }
            if(in_array($date,$this->special_work_day())) {
                $day++;
                continue;
            }
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            if($type==1 && $date==date('Y-m-d',$nstart)){//离职后还有国家规定的节日
                continue;
            }
            if($type==0 && $date==date('Y-m-d',$nend)){
                continue;
            }
            $day++;
        }

/*
        for ($i = 1; $i <= $justday; $i++) {
            $cacluate = mktime(0,0,0,$month,$i+date('d',$nstart),$year);
            $date = date('Y-m-d',$cacluate);
            $weekday = date('w',$cacluate);
            if(in_array($date,$holiday) && ($weekday == 0 || $weekday == 6)) {
                $day++;
                continue;
            }
            if(in_array($date,$take_holiday)) {
                //$day++; add 2012 10 25
                continue;
            }
            if(in_array($date,$this->special_work_day())) {
                $day++;
                continue;
            }
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $day++;
        }*/
        if(in_array($start,$holiday) || in_array($date,$this->special_work_day())){//如果第一天是需要上班，非是节假日
           // $day++;
        }
        return $day;
    }

    /**
     * 新计算实际的请假的天数,现时间点改为9,14,18三个时间点，
     * 该功能为获取自然日，包括周末，但不包含节假日
     * @param string $start
     * @param string $end
     * @return int
     * */
    public function get_nature_many_day($start,$end) {
        $result = '';
        $nstart = $this->format_time($start);
        $nend = $this->format_time($end);
        $start_last = substr($start,11,5);
        $end_last = substr($end,11,5);
        $justday = ($nend - $nstart)/86400;
        //排除周末及特殊节假日
        $day = 0;
        $month = date('m',$nstart);
        $year = date('Y',$nstart);
        for ($i = 0; $i < $justday; $i++) {
            $cacluate = mktime(0,0,0,$month,$i+date('d',$nstart),$year);

            $date = date('Y-m-d',$cacluate);
            if(in_array($date,$this->each_year_holiday_country())) {
                continue;
            }
            $weekday = date('w',$cacluate);
            $day++;
        }

        if($start_last == '09:00' && $end_last == '18:00') {
            $day += 1;
        } elseif($start_last == '09:00' && $end_last == '14:00') {
            $day += 0.5;
        } elseif($start_last == '14:00' && $end_last == '18:00') {
            $day += 0.5;
        } elseif($start_last == '14:00' && $end_last == '9:00') {
            $day += 0.5;
        } else {
            // 不作额外处理
        }

        return $day;
    }

    /**
     * 该功能为获取自然日，包括周末，包含节假日
     * 新计算实际的请假的天数,现时间点改为9,14,18三个时间点，
     * @param string $start
     * @param string $end
     * @return int
     * */
    public function get_nature_all_many_day($start,$end) {
        $result = '';
        $nstart = $this->format_time($start);
        $nend = $this->format_time($end);
        $start_last = substr($start,11,5);
        $end_last = substr($end,11,5);
        $justday = ($nend - $nstart)/86400;
        //排除周末及特殊节假日
        $day = 0;
        $month = date('m',$nstart);
        $year = date('Y',$nstart);
        for ($i = 0; $i < $justday; $i++) {
            $cacluate = mktime(0,0,0,$month,$i+date('d',$nstart),$year);
            $date = date('Y-m-d',$cacluate);
            $weekday = date('w',$cacluate);
            $day++;
        }

        if($start_last == '09:00' && $end_last == '18:00') {
            $day += 1;
        } elseif($start_last == '09:00' && $end_last == '14:00') {
            $day += 0.5;
        } elseif($start_last == '14:00' && $end_last == '18:00') {
            $day += 0.5;
        } elseif($start_last == '14:00' && $end_last == '9:00') {
            $day += 0.5;
        } else {
            // 不作额外处理
        }

        return $day;
    }

    /**
     * 产假特殊计算时间天数
     * 新计算实际的请假的天数,现时间点改为9,14,18三个时间点，
     * @param string $start
     * @param string $end
     * @return int
     * */
    public function get_baby_many_day($start,$end) {
        $result = '';
        $nstart = $this->format_time($start);
        $nend = $this->format_time($end);
        $start_last = substr($start,11,5);
        $end_last = substr($end,11,5);
        $justday = ($nend - $nstart)/86400;
        //排除周末及特殊节假日
        $day = 0;
        $month = date('m',$nstart);
        $year = date('Y',$nstart);
        for ($i = 0; $i < $justday; $i++) {
            $cacluate = mktime(0,0,0,$month,$i+date('d',$nstart),$year);

            $date = date('Y-m-d',$cacluate);
            //产假98天后法定节假日顺延
            if($day > 98 && in_array($date,$this->each_year_holiday_country())) {
                continue;
            }
            $weekday = date('w',$cacluate);
            $day++;
        }
        $day += $this->caculate_half_day($start_last, $end_last);

        return $day;
    }

    /**
     * 半天特殊情况处理
     * @param string $start_last
     * @param string $end_last
     * @return int
     * **/
    public function caculate_half_day($start_last,$end_last) {
        if($start_last == '09:00' && $end_last == '18:00') {
            $day = 1;
        } elseif($start_last == '09:00' && $end_last == '14:00') {
            $day = 0.5;
        } elseif($start_last == '14:00' && $end_last == '18:00') {
            $day = 0.5;
        } elseif($start_last == '14:00' && $end_last == '9:00') {
            $day = 0.5;
        } else {
            $day = 0;
        }
        return $day;
    }
    /**
     * 当某年某月工作天日期
     * @param daystr 201208
     * @return array array(0=>20120803,1=>20120802)
     */
    public function actual_work_day_by_month_list($daystr) {
        $workday = array();
        if(empty($daystr)) {
            $daystr = date('Ym');
        }

        $year = substr($daystr,0,4);//年
        $month = substr($daystr,4,2);//月
        $days = date("t",mktime(0,0,0,$month,1,$year));

        $holiday = $this->each_year_holiday_country();
        $take_holiday=$this->holiday_country();
        $special_workday = $this->special_work_day();
        for ($i = 1; $i<=$days; $i++) {
            $cacluate = mktime(0,0,0,$month,$i,$year);

            $date = date('Y-m-d',$cacluate);

            $weekday = date('w',$cacluate);
            if(in_array($date,$holiday)){
                continue;
            }
            if(in_array($date,$take_holiday)) {
                continue;
            }
            if(in_array($date,$special_workday)) {
                $workday[]=str_replace('-','',$date);
                continue;
            }
            if($weekday == 0 || $weekday == 6) {
                continue;
            }
            $workday[]=str_replace('-','',$date);
        }
        return $workday;
    }
    /**
     * 每年法定节假日
     *
     * @access public
     * @return void
     */
    private $_each_year_holiday_country;
    public function each_year_holiday_country() {
        if($this->_each_year_holiday_country){
            return $this->_each_year_holiday_country;
        }
        $holiday = array();
        if($this->oa_datatype){
            $temp = $this->bll->get_all_special_days(Const_SpecialDay::NO_NEED_WORK_HOLIDAY);
            foreach($temp as $one){
                $holiday[] = date('Y-m-d',strtotime($one['s_date']));
            }
        }else{
            $holiday = array('2012-05-01','2012-10-01','2012-10-02','2012-10-03','2012-09-30','2012-06-22','2012-04-02','2012-04-03','2012-04-04','2012-04-30');
        }
        $this->_each_year_holiday_country = $holiday;
        return $holiday;
    }
    /**
     *
     * 特殊下班时间
     * 指定下班日期和下班时间
     */
    private $_special_leave_time;
    public function special_leave_time(){
        if($this->_special_leave_time){
            return $this->_special_leave_time;
        }
        $data = array();
        if($this->oa_datatype){
            $temp = $this->bll->get_all_special_days(Const_SpecialDay::NEED_WORK);
            foreach($temp as $one){
                if($one['start_time']!=900 || $one['end_time']!=1800){
                    $l = $one['start_time']<1000?1:2;
                    $start = substr($one['start_time'], 0,$l).':'.substr($one['start_time'], $l,2);
                    $l = $one['end_time']<1000?1:2;
                    $end = substr($one['end_time'], 0,$l).':'.substr($one['end_time'], $l,2);
                    $data[date('Y-m-d',strtotime($one['s_date']))] = array('start_time'=>$start,'leave_time'=>$end);
                }
            }
        }else{
            $data['2012-09-29'] = array('start_time'=>'09:00','leave_time'=>'16:00');
        }
        $this->_special_leave_time = $data;
        return $data;
    }
    /**
     * 特殊工作的天数
     * */
    private $_special_work_day;
    public function special_work_day() {
        if($this->_special_work_day){
            return $this->_special_work_day;
        }
        $swork = array();
        if($this->oa_datatype){
            $temp = $this->bll->get_all_special_days(Const_SpecialDay::NEED_WORK);
            foreach($temp as $one){
                $swork[] = date('Y-m-d',strtotime($one['s_date']));
            }
        }else{
            $swork = array('2012-03-31','2012-04-01','2012-04-28','2012-09-29');
        }
        $this->_special_work_day = $swork;
        return $swork;
    }
    /**
     * 格式化时间
     * @param string $str
     * @return int
     * */
    private function format_time($str){
        return strtotime(substr($str,0,10));
    }

    /**
     * 特殊的倒休日子,20121004导20120929假
     */
    private $_holiday_country;
    public function holiday_country() {
        if($this->_holiday_country){
            return $this->_holiday_country;
        }
        $holiday = array();
        if($this->oa_datatype){
            $temp = $this->bll->get_all_special_days(Const_SpecialDay::NO_NEED_WORK_COMPANY);
            foreach($temp as $one){
                $holiday[] = date('Y-m-d',strtotime($one['s_date']));
            }
        }else{
            $holiday = array('2012-10-04','2012-10-05');
        }
        $this->_holiday_country = $holiday;
        return $holiday;
    }

    public function convert_chinese_week($week){
        $wk = array(0 => '日',1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六');
        return $wk[$week];
    }

    public static function get_instance(){
        if(!self::$instance) {
            self::$instance = new Tools_Tools();
        }
        return self::$instance;
    }

    private function __construct(){
        apf_require_class('Bll_Attendance_SpecialDay');
        apf_require_class('Const_SpecialDay');
        $this->oa_datatype = APF::get_instance()->get_config('datatype');
        $this->bll = new Bll_Attendance_SpecialDay();
    }

    private function __clone(){

    }
}

